﻿/****************************** Module Header ******************************\
 * Module Name:  mxListControl.cs
 * Module Version:  1.0.0 (2012-9-14)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension Form instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Windows.Forms;

namespace UCL.Forms
{
    /// <summary>
    /// ListBox扩展方法
    /// </summary>
    public static class mxListControl
    {
        /// <summary>
        /// 设置数据源
        /// </summary>
        /// <param name="c"></param>
        /// <param name="source"></param>
        public static void SetDataSource(this ListControl c, DataTable source, string displayName = "Name", string valueName = "ID")
        {
            if (source == null || source.Rows.Count == 0) return;
            c.DataSource = source;
            c.DisplayMember = displayName;
            c.ValueMember = valueName;
        }

        /// <summary>
        /// 获取数据源
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="c"></param>
        /// <returns></returns>
        /// <remarks>1.0.0 2013-10-17 sutroon</remarks>
        public static T GetDataSource<T>(this ListControl c)
        {
            return (T)c.DataSource;
        }

        /// <summary>
        /// 设置列表的选中状态
        /// </summary>
        /// <param name="c"></param>
        /// <param name="checkedstate"></param>
        /// <remarks>2013-6-8 by sutroon</remarks>
        public static void SetSelectedAll(this ListBox c, bool checkedstate = true)
        {
            int i = c.Items.Count - 1;
            while (i > -1)
            {
                c.SetSelected(i, checkedstate);
                i--;
            }
        }

        /// <summary>
        /// 设置反选
        /// </summary>
        /// <param name="c"></param>
        /// <remarks>2013-9-25 by sutroon</remarks>
        public static void SetSelectedInverse(this ListBox c)
        {
            int i = c.Items.Count - 1;
            while (i >= 0)
            {
                c.SetSelected(i, !(c.GetSelected(i)));
                i--;
            }
        }

        /// <summary>
        /// 获取列表选中的所有值
        /// </summary>
        /// <param name="c"></param>
        /// <param name="valIndex"></param>
        /// <returns></returns>
        /// <remarks>2013-9-25 by sutroon update 新增valIndex参数</remarks>
        public static IList<string> GetSelectedValues(this ListBox c, int valIndex = 0)
        {
            IList<string> list = new List<string>();
            foreach (object item in c.SelectedItems)
            {
                if (item.GetType() == typeof(string))
                {
                    list.Add(item.ToString());
                    continue;
                }
                if (item.GetType() == typeof(DataRowView) || item.GetType() == typeof(DataRow))
                {
                    list.Add(((DataRowView)item)[valIndex].ToString());
                    continue;
                }
                list.Add(item.ToString());
            }
            return list;
        }

        /// <summary>
        /// 获取选中的值
        /// </summary>
        /// <param name="c"></param>
        /// <param name="valIndex"></param>
        /// <returns></returns>
        /// <remarks>兼容GetSelectedText</remarks>
        public static string GetSelectedValue(this ListControl c, int valIndex = 0)
        {
            object item = c.SelectedValue;
            if (item == null) return c.Text.Length > 0 ? c.Text : string.Empty;
            if (item.GetType() == typeof(DataRowView) || item.GetType() == typeof(DataRow))
            {
                item = ((DataRowView)item)[valIndex];
                return item == null ? string.Empty : item.ToString();
            }
            return item.ToString();
        }

        /// <summary>
        /// 获取选中的属性
        /// </summary>
        /// <param name="c"></param>
        /// <param name="attrName"></param>
        /// <param name="defv"></param>
        /// <returns></returns>
        public static string GetSelectedAttr(this ListControl c, string attrName, string defv = "")
        {
            object item = c.SelectedValue;
            if (item == null) return defv;
            if (item.GetType() == typeof(DataRowView) || item.GetType() == typeof(DataRow)) return ((DataRowView)item)[attrName].ToString();
            return defv;
        }

        /// <summary>
        /// 获取选中的文本
        /// </summary>
        /// <param name="c"></param>
        /// <param name="txtIndex"></param>
        /// <returns></returns>
        public static string GetSelectedText(this ListBox c, int txtIndex = 1)
        {
            object item = c.SelectedItem;
            if (item == null) return string.Empty;
            if (item.GetType() == typeof(string)) return item.ToString();
            if (item.GetType() == typeof(DataRowView) || item.GetType() == typeof(DataRow)) return ((DataRowView)item)[txtIndex].ToString();
            return item.ToString();
        }

        #region ListView控件

        /// <summary>
        /// 获取ListView选中的Item中指定属性的值的列表
        /// <para>2013-8-27 by sutroon</para>
        /// <para>如：lvContents.GetSelectedItemsAttrs&lt;string&gt;(a =&gt; a.Text);</para>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lv"></param>
        /// <param name="act"></param>
        /// <returns></returns>
        public static IList<T> GetSelectedItemsAttrs<T>(this ListView lv, Func<ListViewItem, T> act)
        {
            IList<T> lst = new List<T>();
            foreach (ListViewItem li in lv.SelectedItems)
            {
                lst.Add(act(li));
            }
            return lst;
        }
        #endregion
    }


}
